/*
 * 文件名： GuideActivity.java
 * 描    述：首次进入App引导页
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppHelper;
import com.miguo.ui.R;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity {

    private ViewPager mViewPager;
    private ArrayList<View> mViewList;
    private ImageView[] mImageViews;
    private ViewGroup mMainViewGroup;
    private int mViewCount;
    private int mCurSel;
    private LinearLayout mBottomDotLayout;
    private boolean skip = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = getLayoutInflater();
        mViewList = new ArrayList<View>();
        mViewList.add(inflater.inflate(R.layout.layer_guide, null));
        mViewList.add(inflater.inflate(R.layout.layer_guide, null));
        mViewList.add(inflater.inflate(R.layout.layer_guide, null));

        mMainViewGroup = (ViewGroup) inflater.inflate(R.layout.activity_guide,
                null);
        mViewPager = (ViewPager) mMainViewGroup.findViewById(R.id.guidePages);
        // 滑动到顶部和底部时出现的阴影消除 2.3以前用 android:fadingEdge="none"
        if (Build.VERSION.SDK_INT >= 9) {
            mViewPager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }
        mBottomDotLayout = (LinearLayout) mMainViewGroup
                .findViewById(R.id.bottom_dot_layout);
        if (mBottomDotLayout == null) {
            return;
        }
        mViewCount = mViewList.size();
        mImageViews = new ImageView[mViewCount];

        for (int i = 0; i < mViewCount; i++) {
            mImageViews[i] = (ImageView) mBottomDotLayout.getChildAt(i);
            mImageViews[i].setEnabled(true);
            mImageViews[i].setTag(i);
        }
        mCurSel = 0;
        mImageViews[mCurSel].setEnabled(false);

        setContentView(mMainViewGroup);

        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(new GuidePageAdapter());
        mViewPager.addOnPageChangeListener(new GuidePageChangeListener());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void setCurPoint(int index) {
        if (index < 0 || index > mViewCount - 1 || mCurSel == index) {
            return;
        }
        mImageViews[mCurSel].setEnabled(true);
        mImageViews[index].setEnabled(false);
        mCurSel = index;
    }

    class GuidePageAdapter extends PagerAdapter implements OnClickListener {

        private int[] mDrawableIds = new int[]{R.drawable.welcome, R.drawable.welcome, R.drawable.welcome};

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mViewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            View guideImage = mViewList.get(position).findViewById(
                    R.id.guide_iv);
            guideImage.setBackgroundResource(mDrawableIds[position]);
            if (position == 2) {
                View guidell = mViewList.get(position).findViewById(
                        R.id.guide_ll);
                guidell.setVisibility(container.VISIBLE);
                guideImage.setOnClickListener(this);
            }
            return mViewList.get(position);
        }

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.guide_iv:
                    skip = true;
                    AppHelper.StartMain(GuideActivity.this);
                    break;
                default:
                    break;
            }
        }
    }

    class GuidePageChangeListener implements OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageSelected(int position) {
            setCurPoint(position);
            if (position == 2) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (!skip) {
                            AppHelper.StartMain(GuideActivity.this);
                        }
                    }
                }, 1000);
            }
        }
    }
}